﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UMacKeepList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.none };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UMacKeepList";
            }
        }
        
        protected string ShowAgentName
        {
            get
            {
                return HasPower(Permission.经销商平台_经销商) ? OUserController.MutlCmpStr(base.User.UserId) : "";

            }
        }

        protected string searchAgentName
        {
            get
            {
                return GetRequestValue<string>("searchAgentName", "");

            }
        }

        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }


        protected string IntoDateFrom
        {
            get
            {
                return GetRequestValue<string>("IntoDateFrom", "");

            }
        }

        protected string IntoDateTo
        {
            get
            {
                string temp = GetRequestValue<string>("IntoDateTo", "");
                if (string.IsNullOrEmpty(IntoDateFrom))
                    return "";
                if (string.IsNullOrEmpty(temp))
                {
                    temp = Convert.ToDateTime(IntoDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                }

                return temp;

            }
        }

        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "IntoDate");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }
       

        protected string GetSqlWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(ShowAgentName))
            {


                sqlcommonFiled = string.Format("AgentName in {0}", ShowAgentName);// QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, AgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(searchAgentName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, searchAgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            if (IntoDateFrom != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoDate", QueryOperatorsEnum.范围, IntoDateFrom + "|" + IntoDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            return wheresql;
        }





        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Write(" 请转到终端平台 机器保养管理 模块管理查看");
            return;
          //  DataBind();
        }
        protected DataTable sumdt = null;
        protected void DataBind()
        {
           // DataTableCollection dtc = UMacKeepController.GetPagination(SerialNumber, ShowAgentName, IntoDateFrom,IntoDateTo,  pageNum, numPerPage, SQLSort, ref TotalCount);
           // this.rpMenu.DataSource =dtc[0];
            //this.rpMenu.DataBind();

           // sumdt=dtc[1];

           // AddLog(Permission.查看);
        }

        #region
        protected string GetAgentList()
        {
            DataTable AgentMacStockList = UMacKeepController.GetAgentMacNum();

            string litemp = "<li ><a tname=\"{7}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}(<b style='color:red;'>{6}</b>)</a></li>";
            string listr = "";
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetAgentList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot = ShowAgentName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
                DataRow[] drs = AgentMacStockList.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
                string agstock = "0";
                if (drs.Count() > 0)
                {
                    agstock = drs[0]["macnum"].ToString();
                }
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());

                listr += string.Format(litemp, dr["PYAliasName"].ToString() + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check, agstock, agentname);
            }
            return listr;
        }

        protected string GetShowDetailStyle(object x, int KeepHours,object oSerNum)
        {
           // return x.ToString();
            string sHtml = x.ToString();
            string oHtml = "";
            if (!string.IsNullOrEmpty(sHtml))
            {//不为空
                string[] shtmlarr = sHtml.Split(new char[] { '|' });

                oHtml = string.Format("<A  title='保养时间:{1}'>{0}</a>", shtmlarr[0], shtmlarr[1]);
                return oHtml;
            }
            else
            {    //为空则显示点
                //<a class="add" href="UserModel/UMacKeepDetailAdd.aspx" target="dialog" title="添加保养记录" rel="UMacKeepDetailAdd" max=false  mask=true  maxable=false  minable=false  resizable=false drawable=true  width="680" height="500"><span>保养记录</span></a>
                if (HasPower(Permission.新增))
                    return string.Format("<a class=\"add\" href=\"UserModel/UMacKeepDetailSingAdd.aspx?KeepHours={0}&SerialNumber={1}\" target=\"dialog\" title=\"添加保养记录\" rel=\"UMacKeepDetailAdd\" max=false  mask=true  maxable=false  minable=false  resizable=false drawable=true  width=\"450\" height=\"250\">保养</a>", KeepHours, oSerNum);
                else
                    return "";
            }
        }

        protected string GetPer(object x1,object x2)
        {
            if (x1 is DBNull || x2 is DBNull)
                return "0";
            if (x2.ToString() == "0")
                return "0";

            return (Convert.ToDecimal(x1) / Convert.ToDecimal(x2) * 100).ToString("F0");
        }
        
        #endregion

       
    }
}